package com.matt.repository.impl;

import com.matt.foundation.query.NoticeClassesRelQuery;
import com.matt.repository.base.BaseNativeSqlRepository;
import com.matt.repository.base.NoticeClassesRelRepository;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/9/22
 * *******************************
 */
@Component
public class NoticeClassesRelRepositoryImpl extends BaseNativeSqlRepository implements NoticeClassesRelRepository {

    @Override
    public List<Object[]> findNoticeClassRecord(NoticeClassesRelQuery query) {
        String querySql = String.format("SELECT DISTINCT a.id,a.title,a.content,a.created,a.deleted,a.operatorId,a.operatorName,a.headImgUrl,a.areaId,a.noticeType FROM tb_notice a, tb_notice_class b WHERE a.id = b.noticeId and a.deleted =false ");
     /*   if (StringUtils.isNotBlank(query.getClassIdList())) {
            querySql += String.format(" and rel.childrenId='%s' ", query.getChildrenId());
        }
        */
        querySql += " order by a.created desc " ;
        querySql += String.format(" limit %s, %s ", query.getPageNum() * query.getPageSize() ,  query.getPageSize());
        List<Object[]> objectArrayList = sqlArrayList(querySql);
        return objectArrayList;
    }

    @Override
    public Long countRecordsByChildren(NoticeClassesRelQuery query) {
        String querySql = "select count(r.id)  " +
                " from rel_children_birthday_record rel right join birthday_record r on rel.birthdayRecordId = r.id " +
                " WHERE true ";

       /* if (StringUtils.isNotBlank(query.getChildrenId())) {
            querySql += String.format(" and rel.childrenId='%s' ", query.getChildrenId());
        }*/
        List objectArrayList = sqlSingleList(querySql);
        return Long.valueOf(objectArrayList.get(0).toString());
    }
}
